package com.cike.sparkstudy.sql.scala

import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.{Row, SQLContext}

object UDF {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf()
      .setMaster("local")
      .setAppName("UDF")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)

    // 构造模拟数据
    val names = Array("leo","jack","Marry","Tom")
    val namesRDD = sc.parallelize(names,2)
    val namesRowRDD = namesRDD.map{name => Row(name)}
    val structType = StructType(Array(StructField("name",StringType,true)))
    val nameDF = sqlContext.createDataFrame(namesRowRDD,structType)
    //注册一张临时表
    nameDF.registerTempTable("names")
    //定义和注册UDF
    //定义函数：自己写匿名函数
    //注册函数：SQLContext.udf.register()
    sqlContext.udf.register("strLen",(str:String) => str.length)

    //使用自定义函数
    sqlContext.sql("select name,strLen(name) from names")
      .collect()
      .foreach(println)

  }

}
